package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.TakingRecordsAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.TakingRecordsBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;

//接诊记录
public class TakingRecordsActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private ImageButton ib_back;
    private ImageButton ib_list;
    private XRecyclerView recyclerView;
    private TakingRecordsAdapter adapter;
    @Override
    public int intiLayout() {
        return R.layout.activity_takingrecord;
    }

    @Override
    public void initView() {
        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("接诊记录");
        ib_back=findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        ib_list=findViewById(R.id.ib_list);
       // ib_list.setVisibility(View.VISIBLE);
        ib_list.setImageDrawable(getResources().getDrawable(R.drawable.add_light));
        ib_list.setOnClickListener(this);
        recyclerView=findViewById(R.id.takingrecord_recycleview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                    getNetWork();
                /*recyclerView.refreshComplete();*/
            }

            @Override
            public void onLoadMore() {

                recyclerView.loadMoreComplete();
            }

        });
        showLoading();
    }

    @Override
    public void initData() {
        getNetWork();

    }
    private void getNetWork(){
        int ysid= (int) SpUtils.get(TakingRecordsActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        // map.put("jmId",idnumber);
        map.put("ysid",ysid);
        OkHttpUtils.getNetWork(TakingRecordsActivity.this, Constant.baseurl+"online/jzzz/jzjl",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    dismissLoading();
                    String s= (String) msg.obj;

                    try{
                        Gson gson = new GsonBuilder()
                            .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                            .registerTypeAdapter(int.class,new AdapterIntegerDefault0())
                            .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                            .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                            .create();
                        TakingRecordsBean bean=gson.fromJson(s, TakingRecordsBean.class);

                        if(bean.getCode()==0){
                            adapter=new TakingRecordsAdapter(TakingRecordsActivity.this,bean.getData());
                            recyclerView.setAdapter(adapter);
                            adapter.setOnItemClickListener(new TakingRecordsAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int postion) {
                                    Intent intent=new Intent(TakingRecordsActivity.this,FieledMissionsActivity.class);
                                    // intent.putExtra("")
                                    intent.putExtra("jmid",bean.getData().get(postion).getJmId());
                                    intent.putExtra("jzid",bean.getData().get(postion).getId());
                                    intent.putExtra("name",bean.getData().get(postion).getJmxm());//姓名
                                    startActivity(intent);
                                }
                            });

                            recyclerView.refreshComplete();
                        }else{
                            Toast.makeText(TakingRecordsActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(TakingRecordsActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(TakingRecordsActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(TakingRecordsActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                finish();
                break;
            case R.id.ib_list:  //添加
                startActivity(new Intent(TakingRecordsActivity.this,ConsultationActivity.class));
                break;
        }
    }
}
